﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;
using Volo.Abp.EventBus.Distributed;
using System.Linq.Dynamic.Core;

namespace Himp.Platform.Dc
{
    /// <summary>
    /// 患者主索引服务
    /// </summary>
    public class PatnMasterIdxService : DcAppService, IPatnMasterIdxService
    {
        private readonly IRepository<PatnMasterIdx, Guid> _patnBasInfoRepository;
        private readonly PatnMasterIdxManager _patnBasInfoManager;
        private readonly IDistributedEventBus _distributedEventBus;

        public PatnMasterIdxService(IRepository<PatnMasterIdx, Guid> patnBasInfoRepository
            , PatnMasterIdxManager patnBasInfoManager
            , IDistributedEventBus distributedEventBus)
        {
            _patnBasInfoRepository = patnBasInfoRepository;
            _distributedEventBus = distributedEventBus;
            _patnBasInfoManager = patnBasInfoManager;
        }

        /// <summary>
        /// 查询患者主索引
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<PatnMasterIdxDto>> GetListAsync(PatnMasterIdxInput input)
        {
            var iQuery = await _patnBasInfoRepository.GetQueryableAsync();
            iQuery = iQuery.WhereIf(!string.IsNullOrEmpty(input.OtpNo), d => d.PatnMasterIdxAssocOtpNos.Any(p => p.OtpNo == input.OtpNo));
            iQuery = iQuery.WhereIf(!string.IsNullOrEmpty(input.IptNo), d => d.PatnMasterIdxAssocIptNos.Any(p => p.IptNo == input.IptNo));
            iQuery = iQuery.WhereIf(!string.IsNullOrEmpty(input.Idcard), d => d.Idcard == input.Idcard);
            iQuery = iQuery.WhereIf(!string.IsNullOrEmpty(input.PatnId), d => d.PatnId == input.PatnId);

            if (input.SkipCount == 0)
            {
                var itemsDto = ObjectMapper.Map<List<PatnMasterIdx>, List<PatnMasterIdxDto>>(iQuery.ToList());

                return new PagedResultDto<PatnMasterIdxDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.SkipCount, input.MaxResultCount);
                var itemsDto = ObjectMapper.Map<List<PatnMasterIdx>, List<PatnMasterIdxDto>>(items.ToList());

                return new PagedResultDto<PatnMasterIdxDto>(totalCount, itemsDto);
            }
        }

        /// <summary>
        /// 获取患者主索引明细
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PatnMasterIdxDto> GetAsync(Guid id)
        {
            var patnBasInfo = await _patnBasInfoRepository.GetAsync(id);
            var itemDto = ObjectMapper.Map<PatnMasterIdx, PatnMasterIdxDto>(patnBasInfo);

            return itemDto;
        }

        /// <summary>
        /// 修改患者主索引信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PatnMasterIdxDto> PutAsync(Guid id, PatnMasterIdxUpdateDto input)
        {
            var clinicPatient = await _patnBasInfoRepository.GetAsync(id);
            ObjectMapper.Map(input, clinicPatient);
            clinicPatient = await _patnBasInfoRepository.UpdateAsync(clinicPatient);
            return ObjectMapper.Map<PatnMasterIdx, PatnMasterIdxDto>(clinicPatient);
        }

        /// <summary>
        /// 创建患者主索引信息
        /// </summary>
        /// <returns></returns>
        public async Task<PatnMasterIdxDto> PostAsync(PatnMasterIdxCreateDto input)
        {
            var patnBasInfo = _patnBasInfoManager.CreatePatnMasterIdx(input.PatnSrc, input.Name, input.Idcard);
            patnBasInfo = await _patnBasInfoRepository.InsertAsync(patnBasInfo);
            var itemDto = ObjectMapper.Map<PatnMasterIdx, PatnMasterIdxDto>(patnBasInfo);

            return itemDto;
        }
    }
}
